package com.rs.service.impl;

import com.rs.service.GwService;
import com.rs.service.IPadExchangeHelpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

@Slf4j
@WebService(endpointInterface = "com.rs.service.GwService",
        serviceName = "gwService",
        portName = "gwServicePort")
public class GwServiceImpl implements GwService {

    @Resource
    private IPadExchangeHelpService padExchangeHelpService;

    @Override
    public String ldtpFileSend(String transId, String fileType,
                               String fileName, byte[] file, int fragId, String ext1) {
        log.info("=====================接收推送附件文件=======================");
        log.info("======transId:" + transId + "====fileType:" + fileType + "====fragId:" + fragId + "====ext1:" + ext1 + "======");
        fileName = new String(fileName.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String state = "0";
        System.out.println("fileName:" + fileName);
        try {
            padExchangeHelpService.doSaveLdtpFileSend(transId, fileType, fileName, file, fragId, ext1);
        } catch (Exception e) {
            e.printStackTrace();
            state = "1";
        }

        StringBuilder xml = new StringBuilder();
        OutputStreamWriter ow = null;
        try {
            xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xml.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:gw=\"urn:gwService\">");
            xml.append("<SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
            xml.append("<gw:ldtpFileSendResponse>");
            xml.append("<result>");
            xml.append("<result>");
            xml.append("<status>").append(state).append("</status>");
            xml.append("<msg>success</msg>");
            xml.append("</result>");
            xml.append("</result>");
            xml.append("</gw:ldtpFileSendResponse>");
            xml.append("</SOAP-ENV:Body>");
            xml.append("</SOAP-ENV:Envelope>");
            Message msg1 = PhaseInterceptorChain.getCurrentMessage();
            HttpServletResponse response = (HttpServletResponse) msg1.get(AbstractHTTPDestination.HTTP_RESPONSE);
            response.setContentType("text/xml;charset=UTF-8");
            response.addHeader("ContentEncoding", "utf-8");
            response.reset();
            ow = new OutputStreamWriter(response.getOutputStream());
            ow.write(xml.toString());
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ow != null) {
                try {
                    ow.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        log.info("=====================接收推送附件文件=======================");
        return xml.toString();
    }


    @Override
    public String ldtpFormSend(String transId, String appId, String formType,
                               String form, String ext1) {
        log.info("=====================接收推送表单=======================");
        log.info("======transId:" + transId + "====appId:" + appId + "====formType:" + formType + "====ext1:" + ext1 + "======");
        String state = "0";
        // 解析推送数据
        if ("2".equals(formType)) {
            try {
                padExchangeHelpService.doParseLdtpFormSend(transId);
            } catch (Exception e) {
                e.printStackTrace();
                state = "1";
            }
        }

        StringBuilder xml = new StringBuilder();
        OutputStreamWriter ow = null;
        try {
            xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xml.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:gw=\"urn:gwService\">");
            xml.append("<SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
            xml.append("<gw:ldtpFileSendResponse>");
            xml.append("<result>");
            xml.append("<result>");
            xml.append("<status>").append(state).append("</status>");
            xml.append("<msg>success</msg>");
            xml.append("</result>");
            xml.append("</result>");
            xml.append("</gw:ldtpFileSendResponse>");
            xml.append("</SOAP-ENV:Body>");
            xml.append("</SOAP-ENV:Envelope>");
            Message msg1 = PhaseInterceptorChain.getCurrentMessage();
            HttpServletResponse response = (HttpServletResponse) msg1.get(AbstractHTTPDestination.HTTP_RESPONSE);
            response.setContentType("text/xml;charset=UTF-8");
            response.addHeader("ContentEncoding", "utf-8");
            response.reset();
            ow = new OutputStreamWriter(response.getOutputStream());
            ow.write(xml.toString());
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ow != null) {
                try {
                    ow.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        log.info("=====================接收推送表单=======================");
        return xml.toString();
    }
}
